#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h" //ohos初始化相关库
#include "cmsis_os2.h" //是ARM Cortex-M处理器的通用RTOS接口，为需要RTOS功能的软件组件提供标准化API
#include "ohos_types.h" 

osThreadId_t Task1_ID; //任务1 ID
osThreadId_t Task2_ID; //任务2 ID
//要求：创建两个任务（线程）,任务1正常执行，每隔1s打印一次内容；任务2每隔5s会恢复和挂起任务1
//任务2优先级大于任务1
//注意：任务延时不能使用osDelay,有问题

void Task1(void *argument)
{
    (void)argument;
    while(1)
    {
        printf("Task1......\n");
        sleep(1); //1s
    }
}

void Task2(void *argument)
{
    (void)argument;
    while(1)
    {
        printf("Task 2 starts hanging Task 1\n");
        osThreadSuspend(Task1_ID); //挂起任务1
        sleep(5); //延时5s
        
        printf("Task 2 starts restoring Task 1\n");
        osThreadResume(Task1_ID); //恢复任务1
        sleep(5);
    }
}

//初始化并创建任务
static void kernel_task_example(void)
{
    printf("Enter kernel_task_example\n");

    osThreadAttr_t options;
    options.name = "Task1"; //任务名
    options.attr_bits = 0; //属性位
    options.cb_mem = NULL; //堆空间地址
    options.cb_size = 0; //堆空间大小
    options.stack_mem = NULL; //栈空间地址
    options.stack_size = 1024; //栈空间大小 单位：字节
    options.priority = osPriorityNormal; //任务优先级

    Task1_ID = osThreadNew((osThreadFunc_t)Task1, NULL, &options);
    if(Task1_ID != NULL)
    {
        printf("ID = %d, Create Task1_ID is ok\n",Task1_ID);
    }

    options.name = "Task2";
    options.priority = osPriorityNormal1;
    Task2_ID = osThreadNew((osThreadFunc_t)Task2, NULL, &options); //创建任务2
    if(Task2_ID != NULL)
    {
        printf("ID = %d,Create Task2_ID is ok\n",Task2_ID);
    }
}
SYS_RUN(kernel_task_example);